﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Tip : MonoBehaviour
{
    public void Init(string content)
    {
        var rect = transform.Find("Content").GetComponent<RectTransform>();
        var text = transform.Find("Content/Text").GetComponent<TextMeshProUGUI>();
        text.text = content;
        rect.sizeDelta = new Vector2(text.preferredWidth + 50, text.preferredHeight + 10);
        gameObject.transform.localScale = Vector3.zero;

        Sequence s = DOTween.Sequence();
        s.Append(gameObject.transform.DOScale(1, 0.2f).SetEase(Ease.OutBack));
        s.Append(gameObject.transform.DOMoveY(rect.position.y + 200, 0.5f).SetDelay(1f));
        s.Join(gameObject.transform.DOScale(0, 0.5f));
        s.Play().OnComplete(() => { Destroy(gameObject); });
    }

    public static void Show(string content)
    {
        GameObject go = (GameObject)Resources.Load("Prefab/UIPrefab/Tip");
        go.transform.SetParent(GameObject.Find("Canvas").transform);
        go.transform.localPosition = Vector3.zero;
        RectTransform rectTransform = go.GetComponent<RectTransform>();
        rectTransform.anchorMin = Vector2.zero;
        rectTransform.anchorMax = Vector2.one;
        rectTransform.sizeDelta = Vector2.zero;
        rectTransform.localScale = Vector3.one;

        var tip = go.GetComponent<Tip>();
        tip.Init(content);
    }
}